package middleware

import (
  grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
  "go.uber.org/zap"
  "go.uber.org/zap/zapcore"
  "google.golang.org/grpc/codes"
)

// codeToLevel redirects OK to DEBUG level logging instead of INFO
// This is example how you can log several gRPC code results
func codeToLevel(code codes.Code) zapcore.Level {
	if code == codes.OK {
		// It is DEBUG
		return zap.DebugLevel
	}
	return grpc_zap.DefaultCodeToLevel(code)
}

// AddLogging returns grpc.Server config option that turn on logging.
func ZapLogger(logger *zap.Logger) *zap.Logger {
	// Shared options for the logger, with a custom gRPC code to log level function.

	// Make sure that log statements internal to gRPC library are logged using the zapLogger as well.
	grpc_zap.ReplaceGrpcLoggerV2(logger)

	return logger
}

func ZapOption() []grpc_zap.Option {
  opt := []grpc_zap.Option{
		grpc_zap.WithLevels(codeToLevel),
	}
  return opt
}
